app.controller("scheduleController",function($scope, $http,$filter){
    // 补全信息
    var ajax = new XMLHttpRequest();
    ajax.open("GET", "/OA/staff/teacher", true);
    ajax.onload = function() {
       var list = JSON.parse(ajax.responseText).map(function(i) {
           return i.username});
        new Awesomplete(document.querySelector("#staffname"),{ list: list});
    };
    ajax.send();
    $scope.find=function () {
        var campus_id=$("#schoolcampus").find("option:selected").val();
        var staffname=$scope.staffname;
        var stafftime=$("#date_entry").val();
        stafftime=stafftime.replace(/-/g,'/');
        stafftime=new Date(stafftime);
        stafftime=stafftime.getTime();
        startdate=$filter("date")(stafftime, "yyyy-MM-dd");
        var out_entry=$("#out_entry").val();
        out_entry=out_entry.replace(/-/g,'/');
        out_entry=new Date(out_entry);
        out_entry=out_entry.getTime();
        enddate=$filter("date")(out_entry, "yyyy-MM-dd");
        $scope.json={campus_id:campus_id,staff_name:staffname,startdate:startdate,enddate:enddate};
        console.log($scope.json)
        $http({
            method: 'POST',
            url: '/OA/classone2one/one2one',
            data:$.param($scope.json),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response) {
            console.log(response)               //总条数
            $scope.total = response.data.length;
            //反转函数转化abcd转dcba
            $scope.data = response.data;
            //选择显示的条数
            $scope.values = [{"limit": 6}];
            //默认显示的条数
            $scope.selectedLimit = $scope.values[0];
            //默认显示当前页数
            $scope.currentPage = 1;
            if ($scope.data != null) {
                $scope.datas = $scope.data.slice($scope.selectedLimit.limit * ($scope.currentPage - 1), $scope.selectedLimit.limit * $scope.currentPage);
            } else {
                alert($scope.data);
            }
            $scope.page = {
                "limit": $scope.selectedLimit.limit, "pageSize": 5, "pageNo": $scope.currentPage, "totalCount": $scope.total
            };
        })
    }
    $http({
        method: 'GET',
        url: '/OA/classone2one/one2one',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).success(function (response) {
        console.log(response)               //总条数
        $scope.total = response.data.length;
        //反转函数转化abcd转dcba
        $scope.data = response.data;
        //选择显示的条数
        $scope.values = [{"limit": 6}];
        //默认显示的条数
        $scope.selectedLimit = $scope.values[0];
        //默认显示当前页数
        $scope.currentPage = 1;
        if ($scope.data != null) {
            $scope.datas = $scope.data.slice($scope.selectedLimit.limit * ($scope.currentPage - 1), $scope.selectedLimit.limit * $scope.currentPage);
        } else {
            alert($scope.data);
        }
        $scope.page = {
            "limit": $scope.selectedLimit.limit, "pageSize": 5, "pageNo": $scope.currentPage, "totalCount": $scope.total
        };
    })
    $scope.change = function(selectedLimit){
        $scope.page.limit = selectedLimit.limit;
        $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
    }
    $scope.pageChanged = function(){
        $scope.page.limit = $scope.selectedLimit.limit;
        $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
    }
    $scope.setPage = function (go) {
        $scope.length = Math.ceil($scope.total/$scope.selectedLimit.limit);
        console.log($scope.length);
        if(go > $scope.length){
            $scope.page.pageNo =  $scope.length;
            console.log($scope.length);

        }else{
            $scope.page.pageNo=go;
        }
        $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
    };
});
